<template>
  <Layout class-prefix="layout">
    <Tabs :data-source="recordTypeList"
          :value.sync="record.type"/>
    <Tags @update:value="record.tags = $event"/>
    <FormItem field-name="备注："
              placeholder="在这里输入备注"
              :value.sync="record.notes"/>
    <NumberPad :value.sync="record.amount" @submit="saveRecord"/>
  </Layout>
</template>

<script lang="ts">
import Vue from 'vue';
import Tabs from '@/components/Tabs.vue';
import Tags from '@/components/Money/Tags.vue';
import FormItem from '@/components/Money/FormItem.vue';
import NumberPad from '@/components/Money/NumberPad.vue';
import {Component} from 'vue-property-decorator';
import recordTypeList from '@/constants/recordTypeList';

@Component({
  components: {Tabs, FormItem, Tags, NumberPad},
})
export default class Money extends Vue {
  get recordList() {
    return this.$store.state.recordList;
  }

  recordTypeList = recordTypeList;

  record: RecordItem = {
    tags: [], notes: '', type: '-', amount: 0
  };

  created() {
    this.$store.commit('fetchRecords');
  }

  onUpdateNotes(value: string) {
    this.record.notes = value;
  };

  saveRecord() {
    if(!this.record.tags || this.record.tags.length === 0){
      return window.alert('请至少选择一个标签')
    }
    this.$store.commit('createRecord', this.record);
    if(this.$store.state.createRecordError === null){
      window.alert('已保存')
      this.record.notes = ''
    }
  };
};
</script>

